package com.e3.basdoc.goodscls.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.goodscls.dao.GoodsClsDAO;
import com.e3.basdoc.goodscls.entity.GoodsClsEntity;
import com.e3.basdoc.goodscls.model.GoodsClsModel;
import com.e3.basdoc.goodscls.service.GoodsClsCacheManager;
import com.e3.basdoc.goodscls.service.GoodsClsService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class GoodsClsServiceImpl extends SuperService implements	GoodsClsService {
	
	private GoodsClsDAO goodsClsDAO;
	
	public GoodsClsDAO getGoodsClsDAO(){
		if(goodsClsDAO==null){
			goodsClsDAO = DAOHelper.createDAOInstance(GoodsClsDAO.class);
		}
		return goodsClsDAO;
	}

	public GoodsClsModel newGoodsCls(GoodsClsModel model) throws ServiceException {
		validateGoodsCls(model);
		checkGoodsClsExisted(model);
		
		GoodsClsEntity entity = modelToEntity(model, null);
		getGoodsClsDAO().persist(entity);
		
		model.setId(entity.getId());
		
		GoodsClsCacheManager.INSTANCE.put(model);
		
		return model;
	}
	
	public GoodsClsModel modifyGoodsCls(GoodsClsModel model) throws ServiceException{
		validateGoodsCls(model);
		
		long id = model.getId();

		GoodsClsEntity entity = getGoodsClsDAO().getById(GoodsClsEntity.class, id);
		if (entity == null) {
			return newGoodsCls(model);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(model.getCode())) {
			checkGoodsClsExisted(model);
		}

		modelToEntity(model, entity);
		getGoodsClsDAO().merge(entity);
		
		GoodsClsCacheManager.INSTANCE.put(model);
		return model;
	}
	
	public void deleteGoodsClsByIds(List<Long> ids) throws ServiceException{
		List<Long> deepIds = getDeepChildrenIds(ids);
		getGoodsClsDAO().deleteGoodsClsByIds(deepIds);
		
		GoodsClsCacheManager.INSTANCE.remove(deepIds);
	}
	
	private List<Long> getDeepChildrenIds(List<Long> goodsClsIds) throws ServiceException{
		if(goodsClsIds==null||goodsClsIds.isEmpty()){
			return null;
		}
		List<Long> ids = new ArrayList<Long>();
		for (long id : goodsClsIds) {
			if(!ids.contains(id)){
				ids.add(id);
			}
		}
		List<Long> childrenIds = getGoodsClsDAO().getChildrenIds(goodsClsIds);
		if(childrenIds!=null&&childrenIds.size()>0){
			List<Long> cIds = getDeepChildrenIds(childrenIds);
			for (Long id : cIds) {
				if(!ids.contains(id)){
					ids.add(id);
				}
			}
		}
		return ids; 
	}
	
	public void checkGoodsClsExisted(GoodsClsModel model) throws ServiceException {
		if (model == null) {
			return;
		}
		String code = model.getCode();
		String name = model.getName();

		StringBuffer msgs = new StringBuffer();
		long size = getGoodsClsDAO().getSizeByCode(code);
		if(size>0){
			msgs.append("编码为'" + code+"' ");
		}
		size = getGoodsClsDAO().getSizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的编码和名称。");
			throw new ServiceException(msgs.toString());
		}
	}
	
	private void validateGoodsCls(GoodsClsModel model) throws ServiceException{
		String code = model.getCode();
		String name = model.getName();

		if (code == null || code.trim().length() == 0 || name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置编码和名称。");
		}
	}

	private GoodsClsEntity modelToEntity(GoodsClsModel model, GoodsClsEntity entity) {
		entity = ocModelToEntity(GoodsClsEntity.class, model, entity);

		entity.setCode(model.getCode());
		entity.setName(model.getName());
		entity.setAverageCost(model.getAverageCost());
		entity.setAverageLeadtime(model.getAverageLeadtime());
		entity.setAveragePrice(model.getAveragePrice());
		entity.setRemark(model.getRemark());
		entity.setParentId(model.getParentId());
		
		return entity;
	}

}
